package android.slc.appbase.api.slc.config;

import android.slc.box.db.sp.DpManager;
import de.saxsys.mvvmfx.internal.viewloader.ResourceBundleManager;
import io.reactivex.rxjava3.annotations.NonNull;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by on the way on 2018/9/18.
 */

public class ApiConfig extends DpManager {

    private String token = null;
    private int pageSize = Value.DEF_PAGE_SIZE;

    private String serviceApiAddress;
    private String socketAddress;
    private int socketPort;

    private static class Holder {
        private static final ApiConfig instance = new ApiConfig();
    }

    public static ApiConfig getInstance() {
        return Holder.instance;
    }


    private ApiConfig() {
        super("api_config");
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getToken() {
        return dp.getString(Config.KEY_TOKEN);
    }

    public void setToken(String token) {
        dp.putValue(Config.KEY_TOKEN, token);
    }

    public String getServiceApiAddress() {
        if (isApplyCacheApi()) {
            return getServiceApiAddressByDp(serviceApiAddress);
        }
        return serviceApiAddress;
    }

    public void setServiceApiAddress(String serviceApiAddress) {
        this.serviceApiAddress = serviceApiAddress;
        setServiceApiAddressByDp(serviceApiAddress, false);
    }

    public String getSocketAddress() {
        if (isApplyCacheApi()) {
            return getSocketAddressByDp(socketAddress);
        }
        return socketAddress;
    }

    public void setSocketAddress(String socketAddress) {
        this.socketAddress = socketAddress;
        setSocketAddressByDp(socketAddress, false);
    }

    public int getSocketPort() {
        if (isApplyCacheApi()) {
            return getSocketPortByDp(socketPort);
        }
        return socketPort;
    }

    public void setSocketPort(int socketPort) {
        this.socketPort = socketPort;
        setSocketPortByDp(socketPort, false);
    }

    public static class Key {
        public static final String KEY_AUTO_SEARCH = "autoSearch";
        public static final String KEY_TOKEN = "Authorization";
    }

    public static class Value {
        public static final String API_DOMAIN_NAME = "domain-api";
        //默认的每页个数
        private static final int DEF_PAGE_SIZE = 15;
        //后台定义
        public static final int SUCCEED = 200;//成功
        public static final int ERROR_REQUEST = 400;//后台提示
        public static final int NORMAL_UNAUTHORIZED = 401;//请求未授权
        public static final int SERVER_ERROR = 500;//后台提示
        public static final int NOT_IMPLEMENTED = 501;//已知错误
        //客户端定义
        public static final int RESULT_UPLOAD_FAILURE = 30003;//上传文件失败
        public static final String RESULT_UPLOAD_FAILURE_MSG = ResourceBundleManager.getInstance().getGlobalResourceBundle().getString("label_file_upload_bg_file_failed");//返回结果list为空提示
    }

    public String getStringTrim(@NonNull final String key, final String defaultValue) {
        String value = dp.getString(key, defaultValue);
        return StringUtils.trim(value);
    }

    //apply
    public boolean isApplyCacheApi() {
        try {
            return dp.getBoolean(Config.KEY_APPLY_CACHE_API, false);
        } catch (Exception e) {
            return false;
        }
    }

    public void setApplyCacheApi(boolean applyCacheApi) {
        dp.putValue(Config.KEY_APPLY_CACHE_API, applyCacheApi);
    }

    //service
    public int getServiceMode() {
        return getServiceMode(Config.CONFIG_MODE_HM);
    }

    public int getServiceMode(int defValue) {
        return dp.getInt(Config.KEY_SERVICE_MODE, defValue);
    }

    public void setServiceMode(int value) {
        dp.putValue(Config.KEY_SERVICE_MODE, value);
    }

    public String getServiceApiAddressByDp() {
        return getServiceApiAddressByDp(null);
    }

    public String getServiceApiAddressByDp(String defValue) {
        return getStringTrim(Config.KEY_SERVICE_API_ADDRESS, defValue);
    }

    public void setServiceApiAddressByDp(String value) {
        setServiceApiAddressByDp(value, true);
    }

    public void setServiceApiAddressByDp(String value, boolean forceSave) {
        if (forceSave || StringUtils.isBlank(getServiceApiAddressByDp())) {
            dp.putValue(Config.KEY_SERVICE_API_ADDRESS, value);
        }
    }

    //socket
    public int getSocketMode() {
        return getSocketMode(Config.CONFIG_MODE_HM);
    }

    public int getSocketMode(int defValue) {
        return dp.getInt(Config.KEY_SOCKET_MODE, defValue);
    }

    public void setSocketMode(int value) {
        dp.putValue(Config.KEY_SOCKET_MODE, value);
    }

    public String getSocketAddressByDp() {
        return getSocketAddressByDp(null);
    }

    public String getSocketAddressByDp(String defValue) {
        return getStringTrim(Config.KEY_SOCKET_ADDRESS, defValue);
    }

    public void setSocketAddressByDp(String value) {
        setSocketAddressByDp(value, true);
    }

    public void setSocketAddressByDp(String value, boolean forceSave) {
        if (forceSave || StringUtils.isBlank(getSocketAddressByDp())) {
            dp.putValue(Config.KEY_SOCKET_ADDRESS, value);
        }
    }

    public int getSocketPortByDp() {
        return getSocketPortByDp(-1);
    }

    public int getSocketPortByDp(int defValue) {
        return dp.getInt(Config.KEY_SOCKET_PORT, defValue);
    }

    public void setSocketPortByDp(int value) {
        setSocketPortByDp(value, true);
    }

    public void setSocketPortByDp(int value, boolean forceSave) {
        if (forceSave || getSocketPortByDp() == -1) {
            dp.putValue(Config.KEY_SOCKET_PORT, value);
        }
    }

    public static class Config {
        //手动
        public final static int CONFIG_MODE_HM = 0;
        //自动
        public final static int CONFIG_MODE_AUTO = 1;
        private final static String SP_NAME = "api_config";
        private final static String KEY_APPLY_CACHE_API = "applyCacheApi";
        private final static String KEY_SERVICE_MODE = "serviceMode";
        private final static String KEY_SERVICE_API_ADDRESS = "serviceApiAddress";
        private final static String KEY_SOCKET_MODE = "socketMode";
        private final static String KEY_SOCKET_ADDRESS = "socketAddress";
        private final static String KEY_SOCKET_PORT = "socketPort";
        private final static String KEY_TOKEN = "token";
    }
}
